//
//  AppDelegate.m
//  17-HomeWork数据存储
//
//  Created by dllo on 16/8/5.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 调研NSFileManager
//    创建文件、文件夹
//    删除文件、文件夹
//    文件 是否存在
//    移动文件
//    拷贝文件
    
    // 找到Documents的目录
    NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [pathsArray firstObject];
    NSLog(@"%@", documentsDirectory);
    // 创建NSFileManager
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // 拼接文件夹路径
    NSString *testDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
    NSString *testDirectory2 = [documentsDirectory stringByAppendingPathComponent:@"test2"];
    NSString *testDirectory3 = [documentsDirectory stringByAppendingPathComponent:@"test3"];
    // 在Documents目录下创建三个个文件夹
    BOOL isCreateTestSuccess = [fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    BOOL isCreateTestSuccess2 = [fileManager createDirectoryAtPath:testDirectory2 withIntermediateDirectories:YES attributes:nil error:nil];
    BOOL isCreateTestSuccess3 = [fileManager createDirectoryAtPath:testDirectory3 withIntermediateDirectories:YES attributes:nil error:nil];
    
    if (isCreateTestSuccess) {
        NSLog(@"创建test文件夹成功");
    } else {
        NSLog(@"创建test文件夹失败");
    }
    if (isCreateTestSuccess2) {
        NSLog(@"创建test2文件夹成功");
    } else {
        NSLog(@"创建test2文件夹失败");
    }

    if (isCreateTestSuccess3) {
        NSLog(@"创建test3文件夹成功");
    } else {
        NSLog(@"创建test3文件夹失败");
    }
    // 拼接三个字符串文件的路径(指定目录为test文件夹下)
    NSString *testPath = [testDirectory stringByAppendingPathComponent:@"test.text"];
    NSString *testPath2 = [testDirectory stringByAppendingPathComponent:@"test2.text"];
    NSString *testPath3 = [testDirectory stringByAppendingPathComponent:@"test3.text"];
    // 将三个文件写入test文件夹中
    NSString *string = @"写入一些内容, write string";
    [fileManager createFileAtPath:testPath contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    [fileManager createFileAtPath:testPath2 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    [fileManager createFileAtPath:testPath3 contents:[string dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    // 获取目录下所有的文件名(这里有两种方法, 都可以)
    NSArray *file = [fileManager subpathsOfDirectoryAtPath: testDirectory error:nil];
    NSLog(@"%@",file);
    NSArray *files = [fileManager subpathsAtPath: testDirectory];
    NSLog(@"%@",files);
    //更改到待操作的目录下
    [fileManager changeCurrentDirectoryPath:[documentsDirectory stringByExpandingTildeInPath]];
    //创建文件fileName文件名称，contents文件的内容，如果开始没有内容可以设置为nil，attributes文件的属性，初始为nil
    NSString * fileName = @"testFileNSFileManager.txt";
    NSString *string1 = @"Hello World";
    [fileManager createFileAtPath:fileName contents:[string1 dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
    // 删除testFileNSFileManager.txt文件
    [fileManager removeItemAtPath:fileName error:nil];
    // 删除test3文件夹
    if ([fileManager fileExistsAtPath:testDirectory3]) {
        NSLog(@"没删前test3文件夹存在");
    } else {
        NSLog(@"没删前test3文件夹不存在");
    }

    BOOL isDeleteTestDirectory3 = [fileManager removeItemAtPath:testDirectory3 error:nil];
    if (isDeleteTestDirectory3) {
        NSLog(@"删除test3成功");
    } else {
        NSLog(@"删除test3失败");
    }
    if ([fileManager fileExistsAtPath:testDirectory3]) {
        NSLog(@"删除后test3文件夹存在");
    } else {
        NSLog(@"删除后test3文件夹不存在");
    }
    // 移动文件test2 到 test2文件夹中
    NSString *testNewDirectory = [testDirectory2 stringByAppendingPathComponent:@"test2.txt"];
    BOOL isMoveTest2 = [fileManager moveItemAtPath:testPath2 toPath:testNewDirectory error:nil];
    if (isMoveTest2) {
        NSLog(@"移动test2文件成功");
    } else {
        NSLog(@"移动test2文件失败");
    }
    
    BOOL isCopySuccess = [fileManager copyItemAtPath:testPath toPath:testDirectory2 error:nil];
    if (isCopySuccess) {
        NSLog(@"复制成功");
    } else {
        NSLog(@"复制失败");
    }
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
